﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
namespace EHTWebsiteProject.Administrator
{
    public partial class EHTHoTro : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private String message = "";
        private Int32 HotroID = HangSo.DEFAULT_ID;
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    Session["NgonNgu"] = languageCode;
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }
                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if (message != "")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }
                try
                {
                    HotroID = Int32.Parse(Request.QueryString["HotroID"].ToString());
                    
                }
                catch
                {

                }
                if (HotroID != -1)
                {
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    ViewState["HoTroID"] = HotroID;
                    HoTro theHoTro = HoTroProvider.GetHoTroByHoTroID(HotroID);
                    txtHotline.Text = theHoTro.Hotline.ToString();
                    txtPhone.Text = theHoTro.Phone.ToString();
                    txtSkype.Text = theHoTro.Skype;
                    txtTen.Text = theHoTro.GhiChuVN;
                    txtYahoo.Text = theHoTro.Yahoo;
                }
                else
                    ViewState["action"] = HangSo.ACTION_ADD;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                LoadHoTro();
                if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
                    plThongtinHoTro.GroupingText = HangSo.SupportInfor;
                else
                    plThongtinHoTro.GroupingText = HangSo.SupportInforOther;
            }            
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbDichVu.Text = rm.GetString("lbDichVu", ci);
            lbDel.Text = rm.GetString("lbDel", ci);
            lbBat.Text = rm.GetString("lbBat", ci);
            lbEdit.Text = rm.GetString("lbEdit", ci);
            lbTat.Text = rm.GetString("lbTat", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);            
            lbThongTinCongTy.Text = rm.GetString("lbThongTinCongTy", ci);
            lbHoTro.Text = rm.GetString("lbHoTro", ci);
            lbTen.Text = rm.GetString("lbTen", ci);
            ltrDienthoai.Text = rm.GetString("ltrDienthoai", ci);
            lbHoTroManager.Text = rm.GetString("lbHoTroManager",ci);
            lbHinhANhCongTy.Text = rm.GetString("lbHinhANhCongTy", ci);

        }

        public void LoadHoTro()
        {
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                rpHotro.DataSource = HoTroProvider.GetHoTros(); 
                rpHotro.DataBind();
            }
            else
            {
                rpHotroOther.DataSource = HoTroProvider.GetHoTros(); 
                rpHotroOther.DataBind();
            }
        }

        protected void ImaTat_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpHotro.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpHotroOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }

            for (int i = 0; i < arr.Count; i++)
                HoTroProvider.ChangePublish(Int32.Parse(arr[i].ToString()), false);
            LoadHoTro();
        }

        protected void ImgBat_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpHotro.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpHotroOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }


            for (int i = 0; i < arr.Count; i++)
                HoTroProvider.ChangePublish(Int32.Parse(arr[i].ToString()), true);
            LoadHoTro();
        }

        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpHotro.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpHotroOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }

            for (int i = 0; i < arr.Count; i++)
                HoTroProvider.DeleteHoTroByHoTroID(Int32.Parse(arr[i].ToString()));
            LoadHoTro();
        }

        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            Int32 ID = 0;
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpHotro.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        Count++;
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpHotroOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        Count++;
                    }
                }
            }

            if (Count > 1 || Count == 0)
            {
                String messa = "";
                if (IsVN == true)
                    messa = HangSo.Eorr_Edit;
                else
                    messa = HangSo.Eorr_EditOther;
                ltrErorr.Text = HelpMethod.ErorrLogin(messa);
            }
            else
            {
                Response.Redirect("EHTHoTro.aspx?HoTroID=" + ID.ToString());
            }
        }

        protected void rpHotro_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lbStatus = (Label)e.Item.FindControl("lbStatus");
            Image ImagStatus = (Image)e.Item.FindControl("ImagStatus");
            try
            {
                if (lbStatus.Text == "True")
                    ImagStatus.ImageUrl = "~/images/tick.png";
                else
                    ImagStatus.ImageUrl = "~/images/Close.png";
            }
            catch
            {

            }
        }
        public void CheckAll(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpHotro.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpHotro.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
            }
            else
            {
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpHotroOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpHotroOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
            }
        }

        public void Clear()
        {
            txtHotline.Text = "";
            txtPhone.Text = "";
            txtSkype.Text = "";
            txtTen.Text = "";
            txtYahoo.Text = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();

            HoTro theHotro = new HoTro();
            theHotro.GhiChuVN = txtTen.Text;
            theHotro.Phone = txtPhone.Text;            
            theHotro.Room = "1";
            theHotro.Yahoo = txtYahoo.Text;
            theHotro.Skype = txtSkype.Text;
            theHotro.Hotline = txtHotline.Text;
            theHotro.Gtalk = "";
            if (action == HangSo.ACTION_ADD)
            {
                theHotro.HoTroID = -1;
                HoTroProvider.IUHoTro(theHotro, true);
            }
            else
            {
                theHotro.HoTroID = Int32.Parse(ViewState["HoTroID"].ToString());
                HoTroProvider.IUHoTro(theHotro, false);
            }
            Clear();
            Response.Redirect("EHTHoTro.aspx");
        }

        protected void rpHotroOther_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lbStatus = (Label)e.Item.FindControl("lbStatus");
            Image ImagStatus = (Image)e.Item.FindControl("ImagStatus");
            try
            {
                if (lbStatus.Text == "True")
                    ImagStatus.ImageUrl = "~/images/tick.png";
                else
                    ImagStatus.ImageUrl = "~/images/Close.png";
            }
            catch
            {

            }
        }
    }
}